package com.gla.service;

import com.gla.dao.CommodityMapper;
import com.gla.dao.EvaluationMapper;
import com.gla.dao.ShopMapper;
import com.gla.dao.TypeMapper;
import com.gla.dto.ShopMore;
import com.gla.entity.Commodity;
import com.gla.entity.Shop;
import com.gla.entity.Type;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class FYHService {
    @Resource
    ShopMapper shopMapper;
    @Resource
    CommodityMapper commodityMapper;
    @Resource
    TypeMapper typeMapper;
    @Resource
    EvaluationMapper evaluationMapper;

    public List<Shop> selectAllShop(){
        return shopMapper.selectAllShop();
    }

    public ShopMore selectShopMore(int shopid){
        Shop shop = shopMapper.selectShopById(shopid);
        List<Commodity> lc0 = commodityMapper.selectCommodity0ByShopId(shopid);
        List<Commodity> lc1 = commodityMapper.selectCommodity1ByShopId(shopid);
        return new ShopMore(shop, lc0, lc1);
    }

    public List<Integer> selectEvaluationByPoint(int shopid){
        List<Integer> li = new LinkedList();
        for(int i = 5; i >= 1; i--) li.add(evaluationMapper.selectEvaluationByPoint(i, shopid));
        return li;
    }

    public List<Shop> delShopById(int id){
        shopMapper.delShopById(id);
        return selectAllShop();
    }

    public List<Shop> updateShopById(Shop shop){
        shopMapper.updateShopById(shop.getId(), shop.getName(), shop.getAddress(), shop.getLongitude(), shop.getLatitude(), shop.getPhoto(), shop.getIntroduction());
        return selectAllShop();
    }

    public Shop getOneShop(int id){
        return shopMapper.selectShopById(id);
    }

    public List<Shop> insertShop(Shop shop){
        shopMapper.insertShop(shop.getName(), shop.getAddress(), shop.getLongitude(), shop.getLatitude(), shop.getPhoto(), shop.getIntroduction());
        return selectAllShop();
    }

    public ShopMore delCommodityById(int id, int sid){
        commodityMapper.delCommodityById(id);
        return selectShopMore(sid);
    }

    public ShopMore updateCommodity(Commodity commodity){
        commodityMapper.updateCommoditByIdPOU(commodity.getId(),commodity.getName(),commodity.getPrice(),commodity.getNumber(),commodity.getP_o_u(),commodity.getType_id(),commodity.getDetails(),commodity.getIntroduction(),commodity.getPhoto());
        return selectShopMore(commodity.getShop_id());
    }

    public Commodity getOneCommodity(int id){
        return commodityMapper.getCommodityById(id);
    }

    public ShopMore insertCommodity(Commodity commodity){
        commodityMapper.insertCommodit(commodity.getName(), commodity.getPrice(), commodity.getNumber(), commodity.getP_o_u(), commodity.getType_id(), commodity.getShop_id(), commodity.getDetails(), commodity.getIntroduction(), commodity.getPhoto());
        return selectShopMore(commodity.getShop_id());
    }
}
